package com.example.test;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button iv;
    private Button touming,xuan,suofang,pingyi,zonghe,shuxing;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv= (Button) findViewById(R.id.iv);
        touming= (Button) findViewById(R.id.touming);
        xuan= (Button) findViewById(R.id.xuan);
        suofang= (Button) findViewById(R.id.suofang);
        pingyi= (Button) findViewById(R.id.pingyi);
        zonghe= (Button) findViewById(R.id.zonghe);
        shuxing= (Button) findViewById(R.id.shuxing);
        touming.setOnClickListener(this);
        xuan.setOnClickListener(this);
        suofang.setOnClickListener(this);
        pingyi.setOnClickListener(this);
        zonghe.setOnClickListener(this);
        shuxing.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.touming:
                touMing();
                break;
            case R.id.xuan:
                xuAn();
                break;
            case R.id.suofang:
                suoFang();
                break;
            case R.id.pingyi:
                pingYi();
                break;
            case R.id.zonghe:
                zongHe();
                break;
            case R.id.shuxing:
                shuXing();
                break;
        }
    }

    /**
     * 属性动画页面
     */
    private void shuXing() {
        Intent i=new Intent(MainActivity.this,ObjectActivity.class);
        startActivity(i);
    }

    /**
     * 综合动画
     */
    private void zongHe() {
        AnimationSet set=new AnimationSet(false);//如果为true这几个动画将采用相同的模式播放，false独立的按照自己的方式，快慢，播放
        ScaleAnimation sca=new ScaleAnimation(1f,1.4f,1f,1.4f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
        sca.setDuration(500);
        sca.setRepeatCount(Animation.INFINITE);
        sca.setRepeatMode(Animation.REVERSE);
        RotateAnimation roa=new RotateAnimation(0,360,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);//0到360度
        roa.setDuration(500);
        roa.setRepeatCount(Animation.INFINITE);
        roa.setRepeatMode(Animation.REVERSE);
        TranslateAnimation tla=new TranslateAnimation(Animation.RELATIVE_TO_PARENT,0,Animation.RELATIVE_TO_PARENT,0.2f,Animation.RELATIVE_TO_PARENT,0,Animation.RELATIVE_TO_PARENT,0.2f);
        tla.setDuration(1000);
        tla.setRepeatCount(Animation.INFINITE);
        tla.setRepeatMode(Animation.REVERSE);
        AlphaAnimation alp=new AlphaAnimation(0f,1f);
        alp.setDuration(1000);
        alp.setRepeatCount(Animation.INFINITE);
        alp.setRepeatMode(Animation.REVERSE);
        set.addAnimation(tla);
        set.addAnimation(roa);
        set.addAnimation(sca);
        set.addAnimation(alp);
        iv.startAnimation(set);
    }

    /**
     * 缩放动画
     */
    private void suoFang() {
        //参数分别为x起始宽度，后来宽度，y起始宽度，后来宽度，x上以自己为中心，0.5f指在自己中心，后面y同理。
        ScaleAnimation sca=new ScaleAnimation(1f,1.4f,1f,1.4f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
        sca.setDuration(500);
        sca.setRepeatCount(Animation.INFINITE);
        sca.setRepeatMode(Animation.REVERSE);
        iv.startAnimation(sca);//注意是startAnimation
    }

    /**
     * 旋转动画
     */
    private void xuAn() {
        RotateAnimation roa=new RotateAnimation(0,360,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);//0到360度
        roa.setDuration(500);
        roa.setRepeatCount(Animation.INFINITE);
        roa.setRepeatMode(Animation.REVERSE);
        iv.startAnimation(roa);
    }


    /**
     * 平移动画
     */
    private void pingYi() {
        //参数0.2f代表父窗体宽度高度的两百倍。Animation.RELATIVE_TO_SELF则是自己宽高的0.2倍
        TranslateAnimation tla=new TranslateAnimation(Animation.RELATIVE_TO_PARENT,0,Animation.RELATIVE_TO_PARENT,0.2f,Animation.RELATIVE_TO_PARENT,0,Animation.RELATIVE_TO_PARENT,0.2f);
        tla.setDuration(1000);
        tla.setRepeatCount(Animation.INFINITE);
        tla.setRepeatMode(Animation.REVERSE);
        iv.startAnimation(tla);
    }

    /**
     * 透明动画
     */
    private void touMing() {
       /* 方式一：代码实现
       AlphaAnimation alp=new AlphaAnimation(0f,1f);
        alp.setDuration(1000);
        alp.setRepeatCount(Animation.INFINITE);
        alp.setRepeatMode(Animation.REVERSE);*/
        //方式二：xml实现。
        Animation alp=AnimationUtils.loadAnimation(this,R.anim.alpha_item);
        iv.startAnimation(alp);
    }
}
